package com.itheima.sfbx.insurance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.framework.commons.utils.EmptyUtil;
import com.itheima.sfbx.framework.commons.utils.ExceptionsUtil;
import com.itheima.sfbx.insurance.dto.CoefficentVO;
import com.itheima.sfbx.insurance.enums.CoefficentEnum;
import com.itheima.sfbx.insurance.enums.SafeguardEnum;
import com.itheima.sfbx.insurance.mapper.CoefficentMapper;
import com.itheima.sfbx.insurance.pojo.Coefficent;
import com.itheima.sfbx.insurance.service.ICoefficentService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CoefficentServiceImpl extends ServiceImpl<CoefficentMapper, Coefficent> implements ICoefficentService {
    @Override
    public Page<CoefficentVO> findPage(CoefficentVO coefficentVO, int pageNum, int pageSize) {

        try {
            LambdaQueryWrapper<Coefficent> queryWrapper = queryWrapper(coefficentVO);
            //分页查询
            Page<Coefficent> coefficentPage = new Page<>();
            //设置分页参数
            Page<Coefficent> page = page(coefficentPage, queryWrapper);
            //转换为VO
            return BeanConv.toPage(page,CoefficentVO.class);
        } catch (Exception e) {
            log.error("查询系数项列表失败：{}", e);
            throw new ProjectException(CoefficentEnum.PAGE_FAIL);
        }
    }

    @NotNull
    private static LambdaQueryWrapper<Coefficent> queryWrapper(CoefficentVO coefficentVO) {
        //构建分页查询对象
        LambdaQueryWrapper<Coefficent> queryWrapper = new LambdaQueryWrapper<>();
        //系数项key精确查询
        queryWrapper.eq(!EmptyUtil.isNullOrEmpty(coefficentVO.getCoefficentKey()),
                Coefficent::getCoefficentKey, coefficentVO.getCoefficentKey());
        //系数项名称模糊查询
        queryWrapper.like(!EmptyUtil.isNullOrEmpty(coefficentVO.getCoefficentKeyName()),
                Coefficent::getCoefficentKeyName, coefficentVO.getCoefficentKeyName());
        //状态查询
        queryWrapper.eq(!EmptyUtil.isNullOrEmpty(coefficentVO.getDataState()),
                Coefficent::getDataState, coefficentVO.getDataState());
        //按更新时间降序
        queryWrapper.orderByDesc(Coefficent::getCreateTime);
        return queryWrapper;
    }

    @Override
    public CoefficentVO findById(String coefficentId) {
        return null;
    }

    @Override
    public CoefficentVO save(CoefficentVO coefficentVO) {
        try {
            //封装成实体对象
            Coefficent coefficent = BeanConv.toBean(coefficentVO, Coefficent.class);
            //保存
            boolean flag = save(coefficent);
            //判断保存结果
            if (!flag){
                throw new RuntimeException("保存系数项失败");
            }
            return null;
        } catch (Exception e) {
            log.error("保存系数项异常：{}", ExceptionsUtil.getStackTraceAsString(e));//获取异常栈信息
            throw new ProjectException(CoefficentEnum.SAVE_FAIL);
        }
    }

    @Override
    public Boolean update(CoefficentVO coefficentVO) {

        try {
            //封装成实体对象
            Coefficent coefficent = BeanConv.toBean(coefficentVO, Coefficent.class);
            boolean flag = updateById(coefficent);
            if(!flag){
                throw new RuntimeException("修改系数项失败");
            }
            return null;
        } catch (Exception e) {
            log.error("修改系数项异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
    }

    @Override
    public Boolean delete(String[] checkedIds) {
        try {
            //将字符串数组转换为Long数组
            List<Long> idsList = Arrays.asList(checkedIds).stream().map(Long::valueOf).collect(Collectors.toList());
            boolean flag = removeByIds(idsList);
            if(!flag){
                throw new RuntimeException("删除系数项失败");
            }
            return null;
        } catch (RuntimeException e) {
            log.error("删除系数项异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(CoefficentEnum.DEL_FAIL);
        }
    }

    @Override
    public List<CoefficentVO> findList(CoefficentVO coefficentVO) {
        try {
            LambdaQueryWrapper<Coefficent> queryWrapper = queryWrapper(coefficentVO);
            List<Coefficent> list = list(queryWrapper);
            if(!EmptyUtil.isNullOrEmpty(list)){
                return BeanConv.toBeanList(list, CoefficentVO.class);
            }
        } catch (Exception e) {
            log.error("查询保障项列表失败；{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SafeguardEnum.LIST_FAIL);
        }
        return List.of();
    }

    @Override
    public CoefficentVO findByCoefficentKey(String coefficentKey) {
        return null;
    }
}
